﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using MySql.Data.MySqlClient;

public class DAL_tbl_qcsicomill_leco_carbon
{
    public DAL_tbl_qcsicomill_leco_carbon()
    {

    }



    public List<tbl_qcsicomill_leco_carbon> select(String QC_Leco_Carbon_ID)
    {
        List<tbl_qcsicomill_leco_carbon> list = new List<tbl_qcsicomill_leco_carbon>();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "Select QC_Leco_Carbon_ID,Work_Order_Number,DRUM_NUMBER,C_VALUE,REMARK,IS_ACTIVE,CREATED_BY,CREATED_DATE,UPDATED_BY,UPDATED_DATE from tbl_qcsicomill_leco_carbon where QC_Leco_Carbon_ID=@QC_Leco_Carbon_ID";
        cmd.Parameters.AddWithValue("@QC_Leco_Carbon_ID", QC_Leco_Carbon_ID);
        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        foreach (DataRow dr in dt.Rows)
        {
            list.Add(new tbl_qcsicomill_leco_carbon()
            {
                QC_Leco_Carbon_ID = dr["QC_Leco_Carbon_ID"].ToString(),
                Work_Order_Number = dr["Work_Order_Number"].ToString(),
                DRUM_NUMBER = dr["DRUM_NUMBER"].ToString(),
                C_VALUE = dr["C_VALUE"].ToString(),
                REMARK = dr["REMARK"].ToString(),
                IS_ACTIVE = Convert.ToBoolean(dr["IS_ACTIVE"]),
                CREATED_BY = dr["CREATED_BY"].ToString(),
                CREATED_DATE = dr["CREATED_DATE"].toDateTimeWithoutError(),
                UPDATED_BY = dr["UPDATED_BY"].ToString(),
                UPDATED_DATE = dr["UPDATED_DATE"].toDateTimeWithoutError()
            });
        }

        return list;
    }



    public List<tbl_qcsicomill_leco_carbon> selectAll()
    {
        List<tbl_qcsicomill_leco_carbon> list = new List<tbl_qcsicomill_leco_carbon>();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "Select QC_Leco_Carbon_ID,Work_Order_Number,DRUM_NUMBER,C_VALUE,REMARK,IS_ACTIVE,CREATED_BY,CREATED_DATE,UPDATED_BY,UPDATED_DATE from tbl_qcsicomill_leco_carbon Order By QC_Leco_Carbon_ID";

        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        foreach (DataRow dr in dt.Rows)
        {
            list.Add(new tbl_qcsicomill_leco_carbon()
            {
                QC_Leco_Carbon_ID = dr["QC_Leco_Carbon_ID"].ToString(),
                Work_Order_Number = dr["Work_Order_Number"].ToString(),
                DRUM_NUMBER = dr["DRUM_NUMBER"].ToString(),
                
                C_VALUE = dr["C_VALUE"].ToString(),
                REMARK = dr["REMARK"].ToString(),
                IS_ACTIVE = Convert.ToBoolean(dr["IS_ACTIVE"]),
                CREATED_BY = dr["CREATED_BY"].ToString(),
                CREATED_DATE = dr["CREATED_DATE"].toDateTimeWithoutError(),
                UPDATED_BY = dr["UPDATED_BY"].ToString(),
                UPDATED_DATE = dr["UPDATED_DATE"].toDateTimeWithoutError()
            });
        }

        return list;
    }

    public DataTable selectAllForGrid()
    {
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = @"Select QC_Leco_Carbon_ID,Work_Order_Number,DRUM_NUMBER,C_VALUE,REMARK,CASE WHEN IS_ACTIVE =1 THEN 'True' ELSE 'False' END as 'IS_ACTIVE',CREATED_BY,CREATED_DATE as CREATED_DATE,UPDATED_BY,UPDATED_DATE from tbl_qcsicomill_leco_carbon Order By QC_Leco_Carbon_ID desc";

        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        return dt;
    }

    public bool insert(List<tbl_qcsicomill_leco_carbon> list)
    {
        bool ret = false;
        List<MySqlCommand> cmds = new List<MySqlCommand>();
        string sql = @"Insert into tbl_qcsicomill_leco_carbon(Work_Order_Number, DRUM_NUMBER, C_VALUE, REMARK, IS_ACTIVE, CREATED_BY, CREATED_DATE)
                        values(@Work_Order_Number, @DRUM_NUMBER, @C_VALUE, @REMARK, @IS_ACTIVE, @CREATED_BY, @CREATED_DATE)";

        foreach (tbl_qcsicomill_leco_carbon row in list)
        {
            MySqlCommand cmd = new MySqlCommand(sql);

            cmd.Parameters.AddWithValue("@Work_Order_Number", row.Work_Order_Number);
            cmd.Parameters.AddWithValue("@DRUM_NUMBER", row.DRUM_NUMBER);
            cmd.Parameters.AddWithValue("@C_VALUE", row.C_VALUE);
            cmd.Parameters.AddWithValue("@REMARK", row.REMARK);
            cmd.Parameters.AddWithValue("@IS_ACTIVE", true);
            cmd.Parameters.AddWithValue("@CREATED_BY", HttpContext.Current.User.Identity.Name);
            cmd.Parameters.AddWithValue("@CREATED_DATE", DateTime.Now);
            cmds.Add(cmd);
        }
        ret = DBUtil.ExecuteNonQuery(cmds);
        return ret;
    }


    public int insert(String Work_Order_Number, String DRUM_NUMBER, String C_VALUE, String REMARK, bool IS_ACTIVE, String CREATED_BY, DateTime CREATED_DATE)
    {
        int i = 0;
        string sql = @"Insert into tbl_qcsicomill_leco_carbon(Work_Order_Number, DRUM_NUMBER, C_VALUE, REMARK, IS_ACTIVE, CREATED_BY, CREATED_DATE)
                        values(@Work_Order_Number, @DRUM_NUMBER, @C_VALUE, @REMARK, @IS_ACTIVE, @CREATED_BY, @CREATED_DATE)";

        MySqlCommand cmd = new MySqlCommand(sql);

        cmd.Parameters.AddWithValue("@Work_Order_Number", Work_Order_Number);
        cmd.Parameters.AddWithValue("@DRUM_NUMBER", DRUM_NUMBER);
        
        cmd.Parameters.AddWithValue("@C_VALUE", C_VALUE);
        cmd.Parameters.AddWithValue("@REMARK", REMARK);
        cmd.Parameters.AddWithValue("@IS_ACTIVE",true);
        cmd.Parameters.AddWithValue("@CREATED_BY", HttpContext.Current.User.Identity.Name);
        cmd.Parameters.AddWithValue("@CREATED_DATE", DateTime.Now);

        i = DBUtil.ExecuteNonQuery(cmd);
        return i;
    }

    public bool update(List<tbl_qcsicomill_leco_carbon> list)
    {
        bool ret = false;
        List<MySqlCommand> cmds = new List<MySqlCommand>();
        string sql = @"Update tbl_qcsicomill_leco_carbon SET Work_Order_Number=@Work_Order_Number, DRUM_NUMBER=@DRUM_NUMBER, C_VALUE=@C_VALUE, REMARK=@REMARK, IS_ACTIVE=@IS_ACTIVE, UPDATED_BY=@UPDATED_BY, UPDATED_DATE=@UPDATED_DATE WHERE QC_Leco_Carbon_ID=@QC_Leco_Carbon_ID";

        foreach (tbl_qcsicomill_leco_carbon row in list)
        {
            MySqlCommand cmd = new MySqlCommand(sql);

            cmd.Parameters.AddWithValue("@QC_Leco_Carbon_ID", row.QC_Leco_Carbon_ID);
            cmd.Parameters.AddWithValue("@Work_Order_Number", row.Work_Order_Number);
            cmd.Parameters.AddWithValue("@DRUM_NUMBER", row.DRUM_NUMBER);
            cmd.Parameters.AddWithValue("@C_VALUE", row.C_VALUE);
            cmd.Parameters.AddWithValue("@REMARK", row.REMARK);
            cmd.Parameters.AddWithValue("@IS_ACTIVE", true);
            cmd.Parameters.AddWithValue("@UPDATED_BY", HttpContext.Current.User.Identity.Name);
            cmd.Parameters.AddWithValue("@UPDATED_DATE", DateTime.Now);

            cmds.Add(cmd);
        }
        ret = DBUtil.ExecuteNonQuery(cmds);
        return ret;
    }


    public int update(String QC_Leco_Carbon_ID, String Work_Order_Number, String DRUM_NUMBER, String C_VALUE, String REMARK, bool IS_ACTIVE, String UPDATED_BY, DateTime UPDATED_DATE)
    {
        int i = 0;
        string sql = @"Update tbl_qcsicomill_leco_carbon SET Work_Order_Number=@Work_Order_Number, DRUM_NUMBER=@DRUM_NUMBER, C_VALUE=@C_VALUE, REMARK=@REMARK, IS_ACTIVE=@IS_ACTIVE, UPDATED_BY=@UPDATED_BY, UPDATED_DATE=@UPDATED_DATE WHERE QC_Leco_Carbon_ID=@QC_Leco_Carbon_ID";

        MySqlCommand cmd = new MySqlCommand(sql);

        cmd.Parameters.AddWithValue("@QC_Leco_Carbon_ID", QC_Leco_Carbon_ID);
        cmd.Parameters.AddWithValue("@Work_Order_Number", Work_Order_Number);
        cmd.Parameters.AddWithValue("@DRUM_NUMBER", DRUM_NUMBER);
        cmd.Parameters.AddWithValue("@C_VALUE", C_VALUE);
        cmd.Parameters.AddWithValue("@REMARK", REMARK);
        cmd.Parameters.AddWithValue("@IS_ACTIVE", true);
        cmd.Parameters.AddWithValue("@UPDATED_BY", HttpContext.Current.User.Identity.Name);
        cmd.Parameters.AddWithValue("@UPDATED_DATE", DateTime.Now);

        i = DBUtil.ExecuteNonQuery(cmd);
        return i;
    }



    public int delete(String QC_Leco_Carbon_ID)
    {
        int i = 0;
        string sql = "Delete FROM tbl_qcsicomill_leco_carbon WHERE QC_Leco_Carbon_ID=@QC_Leco_Carbon_ID";

        MySqlCommand cmd = new MySqlCommand(sql);

        cmd.Parameters.AddWithValue("@QC_Leco_Carbon_ID", QC_Leco_Carbon_ID);

        i = DBUtil.ExecuteNonQuery(cmd);
        return i;
    }


    public List<tbl_qcsicomill_leco_carbon> ddlWorkOrderForParticleSize()
    {
        List<tbl_qcsicomill_leco_carbon> list = new List<tbl_qcsicomill_leco_carbon>();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "Select distinct Work_Order_Number from tbl_qcsicomill_leco_carbon Order By Work_Order_Number";

        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        foreach (DataRow dr in dt.Rows)
        {
            list.Add(new tbl_qcsicomill_leco_carbon()
            {
                
                Work_Order_Number = dr["Work_Order_Number"].ToString(),
                
            });
        }

        return list;
    }


    public List<tbl_qcsicomill_leco_carbon> ddlDrumNumber(string WorkOrderNumber)
    {
        List<tbl_qcsicomill_leco_carbon> list = new List<tbl_qcsicomill_leco_carbon>();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "Select distinct Drum_Number from tbl_qcsicomill_leco_carbon where Work_Order_Number=@Work_Order_Number and Drum_Number<>'' order by Drum_Number";
        cmd.Parameters.AddWithValue("@Work_Order_Number", WorkOrderNumber);
        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        foreach (DataRow dr in dt.Rows)
        {
            list.Add(new tbl_qcsicomill_leco_carbon()
            {
                DRUM_NUMBER = dr["Drum_Number"].ToString()
            });
        }

        return list;
    }

}